package com.party.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.party.domain.Mold;
import com.party.domain.Product;
import com.party.domain.Services;
import com.party.dto.Page;
import com.party.exception.ServiceException;
import com.party.mapper.MoldMapper;
import com.party.mapper.ProductMapper;
import com.party.mapper.ServicesMapper;
import com.party.service.TalentService;

/**
 * 
 * @author Caizhf
 * @date 2017年5月26日下午5:48:17
 * @version v.0.1
 * @email 1115054416@qq.com
 *
 *        <p>
 * 		Description: 目前只完成了达人服务，等待前端包车模板
 *        </p>
 *
 */
@Service
public class TalentServiceImpl implements TalentService {

	@Autowired
	private ServicesMapper servicesMapper;

	@Autowired
	private MoldMapper moldMapper;

	@Autowired
	private ProductMapper productMapper;

	@Override
	public Services showOne(int sid) {
		if (sid == 0)
			throw new ServiceException("传参失败");
		Services s = servicesMapper.findById(sid);
		if (s == null)
			throw new ServiceException("这条记录已经不存在了");
		return s;
	}

	@Override
	public Page showIndex(int pageNum, String product_type, String mold_name) {
		if (StringUtils.isEmpty(product_type) || StringUtils.isEmpty(mold_name))
			throw new ServiceException("传参失败");
		
		int totleRecord = servicesMapper.getTalentTotleRecord(product_type,mold_name);

		Page page = null;
		// 如果传入的页码为空，说明这是第一页
		if (pageNum == 0)
			page = new Page(totleRecord, 1);
		else
			// 如果不为空，那么就按照想看的页码进行
			page = new Page(totleRecord, pageNum);
		List<Services> servicesList = servicesMapper.findAll(product_type, mold_name, page.getStartindex(),
				page.getPAGESIZE());

		if (servicesList == null || servicesList.size() == 0)
			throw new ServiceException("目前数据库中还没有任何记录或者已经超出了查询页数范围");
		page.setList(servicesList);
		return page;
	}

	@Override
	public List<String> showMold(String product_type) {
		if (StringUtils.isEmpty(product_type))
			throw new ServiceException("传参错误，未发现商品类型");
		return moldMapper.findAllByProductType(product_type);
	}

	/**
	 * 插入一个二级服务，需要插2张表，要特别注意逻辑
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void addTalent(Services services) {
		Product pDto = services.getProduct();
		pDto.setAddTime(new Date());
		Mold mDto = services.getMold();
		if (pDto == null || mDto == null)
			throw new ServiceException("请不要传入任何的空值!");
		if (StringUtils.isEmpty(mDto.getMold_name()))
			throw new ServiceException("请先选择分类！");
		// 先有产品基础类
		productMapper.inster(pDto);
		// 插入完成后，POJO需要返回一个主键

		// 如果详细分类名称为空，那么就不选择分类
		Mold mEntity = moldMapper.findByName(mDto.getMold_name());
		if (pDto.getPid() == 0 || mEntity.getMid() == 0)
			throw new ServiceException("存储过程出错!");

		services.setProduct_id(pDto.getPid());
		services.setMold_id(mEntity.getMid());
		servicesMapper.add(services);
	}

	/**
	 * 删除服务表的同时还要删除product表
	 */
	@Override
	@Transactional
	public void deleteTalent(int sid) {
		Services s = servicesMapper.findById(sid);
		if (s != null) {
			//先删除services记录
			servicesMapper.deleteById(sid);
			//再删除级联的product记录
			productMapper.delete(s.getProduct().getPid());
			return ;
		}
		throw new ServiceException("删除失败,这条记录已经不存在数据库了");
	}

}
